﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace footwear
{
    public partial class ManageCategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (DbConnect.Connection == null)
            {
                var dbConnect = new DbConnect();
            }
            if (Request.QueryString["action"] != null && Request.QueryString["action"] != "")
            {
                var categoryBus = new CategoryBus();
                var item = categoryBus.GetCategoryById(Request.QueryString["id"]);
                if (!Page.IsPostBack)
                {
                    txtId.Text = item.Id.ToString(CultureInfo.InvariantCulture);
                    txtType.Text = item.Type;
                    chkMenu.Checked = item.ShowIndex == 1;
                    Button1.Text = "Sửa";
                }
            }
            else
            {
                Button1.Text = "Thêm";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtType.Text))
            {
                var categoryBus = new CategoryBus();
                if (String.IsNullOrEmpty(txtId.Text))
                {
                    categoryBus.AddCategoryById(txtType.Text, chkMenu.Checked ? "1" : "0");
                }
                else
                {
                    categoryBus.UpdateCategoryById(txtId.Text, txtType.Text, chkMenu.Checked ? "1" : "0");
                }
                txtId.Text = "";
                txtType.Text = "";
                chkMenu.Checked = false;
                Button1.Text = "Thêm";
            }
            
        }
    }
}